﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Tab2Sql.Classes
{
    /// <summary>
    ///
    /// </summary>
    public class Diags : Dictionary<long, string>
    {
        int numberOfErrors;
        /// <summary>
        ///
        /// </summary>
        public int NumberOfErrors
        {
            get { return numberOfErrors; }
            set { numberOfErrors = value; }
        }
        DateTime startTime;
        Stopwatch s;
        /// <summary>
        ///
        /// </summary>
        public Diags()
        {
            numberOfErrors = 0;
            s = new Stopwatch();
            s.Start();
            startTime = DateTime.Now;
            AddNote("Diags constructor fired");
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="ms">
        /// A <see cref="System.Int64"/>
        /// </param>
        /// <returns>
        /// A <see cref="System.String"/>
        /// </returns>
        public string GetNote(long ms)
        {
            return GetTime(ms).ToLongTimeString()+ "  " + this[ms].ToString() + " " +ms.ToString();
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/>
        /// </returns>
        public string GetAllNotes()
        {
            AddNote("GetAllNotes()");
            StringBuilder sb = new StringBuilder();
            foreach (long L in this.Keys.ToList<long>())
            {
                sb.AppendLine(GetNote(L));

            }
            
            return sb.ToString();
        }




        private DateTime GetTime(long ms)
        {
            DateTime t2 = startTime.AddMilliseconds((double)ms);
            return t2;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="note">
        /// A <see cref="System.String"/>
        /// </param>
        public void AddNote (string note)
        {
            numberOfErrors++;
            long elapsed = s.ElapsedMilliseconds;
            while (this.ContainsKey (elapsed))
            {
                elapsed++;
            }
            if (!this.ContainsKey (elapsed))
            {
                this.Add (elapsed, note);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="note">
        /// A <see cref="System.Object"/>
        /// </param>
        public void AddNote(object note)
        {
            numberOfErrors++;
            this.Add(s.ElapsedMilliseconds, note.ToString());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="note">
        /// A <see cref="System.Object"/>
        /// </param>
        /// <param name="sender">
        /// A <see cref="System.Object"/>
        /// </param>
        public void AddNote(object note, object sender)
        {
            numberOfErrors++;
            string msg = note.ToString();
            msg += sender.GetType().Name;
            msg += sender.ToString();
            this.Add(s.ElapsedMilliseconds, msg);
        }






    }
}

